<script type="text/javascript">
$(document).ready(function(){
	$("#mode").change(function(){
		var mode = $(this).val();
		if(mode=='month'){
			$("#tr_month").show();
			$("#tr_year").hide();
		}
		else if(mode=='year'){
			$("#tr_month").hide();
			$("#tr_year").show();
		}
	})
})
</script>
<h2>รายงานสรุปแบบสำรวจความคิดเห็น</h2>
<form action="admin_report/report_6" method="get">
	<div id="advancedSearch">
		<table width="95%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td colspan="2"  class="topic_advancedSearch"><img src="images/topic_advancedSearch2.gif" /></td>
		  </tr>		  	 
		  <tr id="tr_month">
			<td width="180"><br>ปี</td>
			<td>
				<select name="start_year">
					<? 
					$cyear= date("Y");
					for($i=0;$i<10;$i++):
						$year=$cyear-$i;
					?>
					<option value="<?=$year;?>" <?=(@$_GET['start_year'] == $year)?"selected=selected":"";?>><?=$year+543;?></option>
					<? endfor;?>					
				</select>							
			</td>
		  </tr>	
		  	<tr id="tr_month">
			<td width="180"><br>เดือน</td>
			<td>
				<select name="month">
					<? 
					$month = get_month();
					foreach($month as $key=>$item):
					?>
					<option value="<?=$key;?>" <?=(@$_GET['month'] == $key)?"selected=selected":"";?>><?=$item;?></option>
					<? endforeach;?>					
				</select>							
			</td>
		  </tr>		 
		  <tr>
			<td>&nbsp;</td>
			<td><input type="submit" name="submit" value="ค้นหา" ></td>
		  </tr>
		</table>
	</div>
</form>

<?php if($_GET):?>
<div id="main-content">
	<table class="tbContainer">
		<tr>
			<td>
				<table border="0" cellpadding="10" cellspacing="0" width="100%"  >
						<tr class="ui-state-error">
							<td colspan="6" class="ui-state-error tdlist" style="text-align: center;">ปี <?=$_GET['start_year']+543?></td>
						</tr>
						<?php foreach($polls as $poll):?>
							<tr>
								<td></td>
							<?php
								$polldetails = $this->polldetail->where('poll_id = '.$poll['id'])->get();
								foreach($polldetails as $polldetail):
							?>
								<td><b><?=$polldetail['name']?></b></td>
							<?php endforeach;?>
								<td><b>รวม</b></td>
							</tr>
						<tr>
							<td class="tdlist"><?=$poll['title']?></td>
							<?php foreach($polldetails as $polldetail):?>
							<td class="tdlist">
								<?php
									$condition = ($_GET['month'] != '00')?" and
									month(pollresults.created) = ".$_GET['month']:"";
									$sql = "SELECT
											count(pollresults.polldetail_id) as rcount
											FROM
											pollresults
											where
											polldetail_id = ".$polldetail['id'].$condition; 
									$pollresult = $this->pollresult->get($sql);
									echo $pollresult[0]['rcount'];
								?>
							</td>
							<?php endforeach?>
							<td class="tdlist">
								<?php
									$all = $this->pollresult->select("count(id) as allcount")->where("poll_id = ".$poll['id'].$condition)->get();
									echo $all[0]['allcount'];
								?>
							</td>
						</tr>
						<?php endforeach;?>
						<!-- <tr>
							<td></td>
							<td>ดีมาก</td>
							<td>ดี</td>
							<td>น้อย</td>
							<td>น้อยมาก</td>							
							<td>รวม</td>
						</tr> -->
						<!-- <tr>
							<td class="tdlist">ข้อมูลที่ได้จากเว็บไซต์นี้ ท่านได้ประโยชน์มากน้อยแค่ไหน</td>
							<td class="tdlist">xxx</td>
							<td class="tdlist">xxx</td>
							<td class="tdlist">xxx</td>
							<td class="tdlist">xxx</td>
							<td class="tdlist">xxx</td>
						</tr> -->																
				</table>				
			</td>
		</tr>
	</table>
</div>
<?php endif;?>  